/**
 * 选人
 */
function FlowLogModule(ui, engine) {
	this.base = Module;
	this.base(ui, engine);
}
FlowLogModule.prototype = new Module();

FlowLogModule.prototype.alert = function() {

};


FlowLogModule.prototype.open=function(){
	console.log("打开流程跟踪窗口……");
	var workId = this.engine.workId;
	var tabHtml='<ul class="nav nav-tabs" role="tablist"><li role="presentation" class="active" ><a href="#flow-log" aria-controls="home"  role="tab" data-toggle="tab">流转日志</a></li><li role="presentation" ><a href="#flow-img" aria-controls="profile"   role="tab" data-toggle="tab">流程图</a></li></ul>	';
	var tabContentHtml='<div class="tab-content" ><div role="tabpanel" class="tab-pane active" id="flow-log"></div><div role="tabpanel" class="tab-pane" id="flow-img" ></div></div>';
	var html='<div class = "nav-tabs-custom">'+tabHtml+tabContentHtml+'</div>';
	dialog.modal1({
		 title:"流程跟踪",
		 width:989,
		 isDrag:true,
		 isFullScreen:true,
		 sourceType:"local",//local 表示内容来源本地, remote 表示内容从网络请求获取
		 htmlContent:html,//若sourceType=local时，htmlContent不能为空
		 //btnName:"确定",//按钮名称
		 closeModalOnBtnFunctionTrigger:true//当btnFunction触发后是否关闭modalDialog
	});
	this.loadGridData("#flow-log");
	this.loadFlowImg("#flow-img");
	this.getActivityNate();
};


FlowLogModule.prototype.loadGridData=function(id){
	var workId = this.engine.workId;

	var flowLogModule=this;
	var creatTable=function(data){
		var th1='<th  width="10%"  >环节名称</th>';
		var th2='<th width="8%">执行人</th>';
		var th3='<th width="10%">部门</th>';
		var th4='<th width="15%">到达时间</th>';
		var th5='<th width="15%" >完成时间</th>';
		var th6='<th width="5%" >耗时</th>';
		var th7='<th width="8%">状态</th>';
		var th8='<th width="20%">意见</th>';
		var th9='<th width="10%">后续处理</th>';
		var tableHtml='<table class="table table-bordered" id="table-record"><tr>'+th1+th2+th3+th4+th5+th6+th7+th8+th9+'</tr></table>';
		var objs =  eval('('+data+')');
		console.log(objs);
		var table=flowLogModule.creatFlowTable(objs,$(tableHtml));
		console.log($(id));
		$(id).append($(table));
	};

	$.ajax({
		url:ctxPath+'/jbpmService/flowLog/findWorkRecord',
		type:'post',
		dataType:'text',
		async : false,
		data:'workId='+workId,
		success:creatTable
	});
};

FlowLogModule.prototype.loadFlowImg=function(id){
	var workId = this.engine.workId;
	var imgHtml = '<img id="flowImg" style="width:auto;height:auto;"src="'+ctxPath+'/jbpmService/flowLog/printImage?workId='+workId+'" /> ';
	$(id).append(imgHtml);
};

/**
 * 当前活跃的环节
 */
FlowLogModule.prototype.getActivityNate=function(){
	var workId = this.engine.workId;
	$.ajax({
		url:ctxPath+'/jbpmService/flowLog/getActivityNate',
		type:'post',
		dataType:'text',
		async : false,
		data:'workId='+workId,
		success:function(data){
			if(data!=''){
				var objs = eval('('+data+')');
				for(var i = 0 ;i< objs.length;i++){
					var obj = objs[i];
					var imgTopPx = $('#flowImg').css('top');
					var imgLeftPx = $('#flowImg').css('left');
					var imgTop = imgTopPx.substring(0,imgTopPx.indexOf('px'))-0;
					imgTop=70;
					var top = imgTop+obj.y+'px';
					var imgLeft = imgLeftPx.substring(0,imgLeftPx.indexOf('px'))-0;
					imgLeft=25;
					var left = imgLeft+obj.x+'px';
					var div = '<div style="position:absolute; border:1px solid red; left:'+left+'; top:'+top+';width:'+obj.width+'px;height:'+obj.height+'px;"></div>';
					$('#flowImg').after(div);
				}
			}
		}
	});
};


FlowLogModule.prototype.creatFlowTable=function(objs,table){
	console.log(table);
 	var trs =[];
 	var rowspan = 1;
	for(var i = 0 ; i < objs.length; i++){
		var item = objs[i];
	 	if(i==0){
			trs[trs.length]='<tr><td>'+item.activityName+'</td><td>'+item.userName+'</td><td>'+item.depName+'</td><td>'+ formatDate('',item.arriveTime)+'</td><td>'+formatDate('',item.finishTime)+'</td><td>'+getTimeConsuming(item.arriveTime,item.finishTime)+'</td><td>'+getState(item.state)+'</td><td>'+item.opinion+'</td><td>'+item.destActivityName+'</td></tr>';
		}else{
			//前一条记录
			var pre = objs[i-1];
			//前一条和这条的活动名称和批次相等，合并列"后续处理"
			if(pre.activityName == item.activityName && pre.batch == item.batch){
				rowspan ++;
				trs[trs.length]='<tr><td >'+item.userName+'</td><td>'+item.depName+'</td><td>'+ formatDate('',item.arriveTime)+'</td><td>'+formatDate('',item.finishTime)+'</td><td>'+getTimeConsuming(item.arriveTime,item.finishTime)+'</td><td>'+getState(item.state)+'</td><td>'+item.opinion+'</td></tr>';
				//最后一条记录
				if(i==objs.length-1){
					var preStart = objs[trs.length-rowspan];
					var preEnd = objs[trs.length-1];
					trs[trs.length-rowspan]='<tr><td rowspan='+rowspan+'>'+preStart.activityName+'</td><td>'+preStart.userName+'</td><td>'+preStart.depName+'</td><td>'+formatDate('',preStart.arriveTime)+'</td><td>'+formatDate('',preStart.finishTime)+'</td><td>'+getTimeConsuming(preStart.arriveTime,preStart.finishTime)+'</td><td>'+getState(preStart.state)+'</td><td>'+preStart.opinion+'</td><td  rowspan='+rowspan+'>'+preEnd.destActivityName+'</td></tr>';
					rowspan=1;
				}
			}else{
				if(rowspan!=1){
					var preStart = objs[trs.length-rowspan];
					var preEnd = objs[trs.length-1];
					//
					trs[trs.length-rowspan]='<tr><td rowspan='+rowspan+'>'+preStart.activityName+'</td><td>'+preStart.userName+'</td><td>'+preStart.depName+'</td><td>'+formatDate('',preStart.arriveTime)+'</td><td>'+formatDate('',preStart.finishTime)+'</td><td>'+getTimeConsuming(item.arriveTime,item.finishTime)+'</td><td>'+getState(preStart.state)+'</td><td>'+preStart.opinion+'</td><td  rowspan='+rowspan+'>'+preEnd.destActivityName+preStart.batch+'</td></tr>';
					rowspan=1;
				}
				trs[trs.length]='<tr><td>'+item.activityName+'</td><td>'+item.userName+'</td><td>'+item.depName+'</td><td>'+formatDate('',item.arriveTime)+'</td><td>'+formatDate('',item.finishTime)+'</td><td>'+getTimeConsuming(item.arriveTime,item.finishTime)+'</td><td>'+getState(item.state)+'</td><td>'+item.opinion+'</td><td>'+item.destActivityName+'</td></tr>';
			}
		}
	}
   	for( var j = 0 ; j<trs.length;j++){
   		table.append(trs[j]);
   	}
   	return table;
};

function getState(stateItem){
	var state="";
	if(stateItem=='finished'){
		state = '完成';
	}else if(stateItem=='ready'){
		state = '正在处理';
	}else if(stateItem=='backed'){
		state = '回退';
	}else if(stateItem=='competitive'){
		state = '抢占';
	}
	return state;
}

function formatDate(formatStr, fdate){
	var fTime, fStr = 'ymdhis';
	if (!formatStr)
		formatStr= "y-m-d h:i:s";
	if (fdate)
		fTime = new Date(fdate);
	else
		return '';
	var formatArr = [
		fTime.getFullYear().toString(),
		(fTime.getMonth()+1).toString(),
		fTime.getDate().toString(),
		fTime.getHours().toString(),
		fTime.getMinutes().toString(),
		fTime.getSeconds().toString()
	];
	for (var i=0; i<formatArr.length; i++){
		if(formatArr[i] < 10){
			//wangxufeng edit
			formatArr[i] = "0"+formatArr[i];
		}
		formatStr = formatStr.replace(fStr.charAt(i), formatArr[i]);
	}
	return formatStr;
}
//wangxufeng add 2014-11-21
//通过到达时间和完成时间得到耗时
function getTimeConsuming(arriveTime,finishTime){
	var aTime,fTime,aymd,fymd;
	aTime = new Date(arriveTime);
	fTime = new Date(finishTime);
	aymd = new Date(aTime.getFullYear(),(aTime.getMonth()+1),aTime.getDate());
	fymd = new Date(fTime.getFullYear(),(fTime.getMonth()+1),fTime.getDate());

	var useHour = fTime.getHours()-aTime.getHours();
	var useMinute = fTime.getMinutes()-aTime.getMinutes();
	var useSecond = fTime.getSeconds()-aTime.getSeconds();
	var day,minute,second;
	day=parseInt(Math.abs(aymd-fymd)/1000/60/60/24);
	//15:23:12
	//13:12:54
	if(useHour>=0){
		hour = useHour;
	}else{
		hour = useHour + 24;
		day = day - 1;
	}
	if(useMinute>=0){
		minute = useMinute;
	}else{
		minute = useMinute + 60;
		hour = hour - 1;
	}
	if(useSecond>=0){
		second = useSecond;
	}else{
		second = useSecond + 60;
		minute = minute - 1;
	}
	return (day>0?day+"天":"")+(hour>0?hour+"小时":"")+(minute>0?minute+"分":"")+(second>0?second+"秒":"");
}